/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package openrule;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.MouseInfo;
import java.awt.Point;
import java.util.Vector;
import javax.swing.JColorChooser;
import openrule.event.RulerEvent;
import openrule.event.RulerListener;

/**
 *
 * @author pborrego
 */
public class Main implements RulerListener {
    private BaseRuler currentRuler;
    private Ruler horizontal = new Ruler();
    private RulerVert vertical = new RulerVert();
    
    public Main() {
        horizontal.addRulerListener(this);
        vertical.addRulerListener(this);
        
        horizontal.setVisible(true);
        vertical.setVisible(false);
        
        currentRuler = horizontal;
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main();
            }
        });
    }

    public void close(RulerEvent evt) {
        // Closes out the application...
        
        horizontal.setVisible(false);
        horizontal.dispose();
        
        vertical.setVisible(false);
        vertical.dispose();
    }

    public void minimize(RulerEvent evt) {
        // mimimizes the correct ruler...
        currentRuler.setState(Frame.ICONIFIED);
    }

    public void flip(RulerEvent evt) {
        Dimension dh = horizontal.getSize();
        Dimension dv = vertical.getSize();    

        int dx = (int)Math.abs((dv.height - dh.height)/2);
        int dy = (int)Math.abs((dv.width - dh.width)/2);
            
        if(evt.getSource().equals(horizontal))
        {
            Point ph = horizontal.getLocation();
            dv.height = dh.width;
            ph.translate(dx, -dy);
            
            horizontal.setVisible(false);
            vertical.setLocation(ph);
            vertical.setSize(dv);
            vertical.setVisible(true);
            
            currentRuler = vertical;
            return;
        }
        
        Point pv = vertical.getLocation();
        dh.width = dv.height;
        pv.translate(-dx, dy);

        vertical.setVisible(false);    
        horizontal.setLocation(pv);
        horizontal.setSize(dh);
        horizontal.setVisible(true);

        currentRuler = horizontal;
    }

    public void command(RulerEvent evt) {
        if(evt.getEventValue().equals("f"))
        {
            flip(evt);
            return;
        }
        
        if(evt.getEventValue().equals("s"))
        {
            // move the current ruler to where the mouse is
            Point cursorLocation = MouseInfo.getPointerInfo().getLocation();
            
            if(evt.getSource().equals(horizontal))
            {
                cursorLocation.translate(0, -(horizontal.getSize().height - 4));
                horizontal.setLocation(cursorLocation);
                return;
            }
            
            cursorLocation.translate(-(vertical.getSize().width - 4), 0);
            vertical.setLocation(cursorLocation);
            return;
        }
        
        if(evt.getEventValue().equals("c"))
        {
            Color newColor;
            
            // changes the color of the rulers...
            if(evt.getSource().equals(horizontal))
            {
                newColor = JColorChooser.showDialog(horizontal,"Choose Ruler Color", horizontal.getRulerColor());
            }
            else
            {
                newColor = JColorChooser.showDialog(vertical,"Choose Ruler Color", vertical.getRulerColor());
            }
            
            if(newColor != null)
            {
                horizontal.setRulerColor(newColor);
                vertical.setRulerColor(newColor);
            }
            return;
        }
        
        if(evt.getEventValue().equals("a"))
        {
            if(evt.getSource().equals(horizontal))
            {
                Point ptLoc = horizontal.getLocationOnScreen();
                ptLoc.translate(0, horizontal.getSize().height);
                
                Preferences p = new Preferences(new Vector<String>());
                p.setLocation(ptLoc);
                p.setVisible(true);
                
                return;
            }

            Point ptLoc = vertical.getLocationOnScreen();
            ptLoc.translate(vertical.getSize().width, 0);

            Preferences p = new Preferences(new Vector<String>());
            p.setLocation(ptLoc);
            p.setVisible(true);
            return;
        }
    }

    public void nudge(RulerEvent evt) {
        Point ptTranslation;

        if(evt.getEventValue().equals("up"))
        {
            ptTranslation = new Point(0, -1);
        }
        else if(evt.getEventValue().equals("down"))
        {
            ptTranslation = new Point(0, 1);
        }
        else if(evt.getEventValue().equals("left"))
        {
            ptTranslation = new Point(-1, 0);
        }
        else if(evt.getEventValue().equals("right"))
        {
            ptTranslation = new Point(1, 0);
        }
        else
        {
            ptTranslation = new Point(0, 0);
        }
              
        Point ptLoc = currentRuler.getLocation();
        ptLoc.translate(ptTranslation.x, ptTranslation.y);
        currentRuler.setLocation(ptLoc);
    }
}
